package com.labichaoka.chaoka.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.gyf.barlibrary.ImmersionBar;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.ui.account.LoginActivity;
import com.labichaoka.chaoka.utils.Density;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseActivity extends PermissionActivity {
    protected Context mContext;
    protected Unbinder mUnbinder;
    protected Typeface iconfont;
    protected KProgressHUD hud;

    protected ImmersionBar mImmersionBar;

    protected abstract int initLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mImmersionBar = ImmersionBar.with(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorWhite)
                .init();   //所有子类都将继承这些相同的属性

        mContext = this;

        setOrientation();
        setContentView(initLayoutId());
        iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");

        mUnbinder = ButterKnife.bind(this);

        hud = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                //.setLabel("请稍后")
                //.setDetailsLabel("Downloading data")
                .setCancellable(true)
                //.setAnimationSpeed(2)
                .setDimAmount(0.5f);

        initView();
        initData();

    }

    /**
     * 跳转到某个Activity
     */
    protected void gotoActivity(Context mContext, Class<?> toActivityClass, Bundle bundle) {
        Intent intent = new Intent(mContext, toActivityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);
        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    /**
     * 退出到某个Activity
     */
    protected void backActivity() {
        finish();
        overridePendingTransition(R.anim.not_exit_push_left_in, R.anim.push_right_out);
    }

    protected void showLoadingProgress() {
        hud.show();
    }
    protected  void hideLoadingProgress(){
        hud.dismiss();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); // 基础指标统计，不能遗漏
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this); // 基础指标统计，不能遗漏
    }

    protected boolean checkIsLogin(){
        if (MyApplication.getInstance().isLogin()){
            //已经登录
            return true;
        }else{
            gotoActivity(mContext, LoginActivity.class,null);
            return false;
        }
    }

    public void setOrientation() {
        Density.setDefault(this);
    }

}
